package 贪心算法;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class 任务调度器 {

    public static int leastInterval(char[] tasks, int n) {
        HashMap<Character,Integer> map = new HashMap();
        int length = tasks.length;
        for (int i = 0; i < length; i++) {
            map.put(tasks[i],map.getOrDefault(tasks[i],0)+1);
        }
        Set<Map.Entry<Character, Integer>> entries = map.entrySet();
        int max = 0;
        for (Map.Entry<Character, Integer> entry : entries) {
            if (entry.getValue() > max){
                max = entry.getValue();
            }
        }
        int num = 0;
        for (Map.Entry<Character, Integer> entry : entries) {
            if (entry.getValue() == max){
                num++;
            }
        }

        return Math.max(length,(max-1)*(n+1)+num);
    }

    public static void main(String[] args) {
        char[] tasks = {'A','A','A','B','B','B'};
        System.out.println(leastInterval(tasks,2));
    }
}
